package com.vip.zj.utils;

import com.sun.deploy.util.StringUtils;

import java.io.*;
import java.util.*;

/**
 * 递归读取文件夹中所有txt文件的内容，方便进行统计等操作
 */
public class ReadFilesUtils {
    public static Map<Integer, Integer> map = new HashMap<>();

    static {
        for (int i = 0; i < 20; i++) {
            map.put(i, 0);
        }
        map.put(9999, 0);
    }

    public static Object ReadFilesUtil(String fileUrl) {
        File file = new File(fileUrl);
        ;
        if (file.isDirectory()) {
            isDir(file);
        } else {
            onlyRead(file);
        }
        return map;
    }

    public static void isDir(File f) {
        f.listFiles();
        for (File file : f.listFiles()) {
            if (file.isDirectory()) {
                isDir(file);
            } else {
                onlyRead(file);
            }
        }

    }

    /**
     * 只读
     *
     * @param file
     * @return
     */
    public static Object onlyRead(File file) {
        if (!file.getName().contains("txt")) {
            return null;
        }
        map.put(9999, map.get(9999) + 1);
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
            String line = null;
            LinkedList<String[]> ll = new LinkedList<>();
            int count = 0;
            while ((line = br.readLine()) != null) {
                String[] linetxt = line.split(" ");
                System.out.println(line+" "+file.getName());
                map.keySet().stream().forEach(integer -> {
                    if (integer.toString().equals(linetxt[0])) {
                        map.put(integer, map.get(integer) + 1);
                    }
                });
            }
        } catch (FileNotFoundException | UnsupportedEncodingException ex) {
        } catch (IOException ex) {
        } finally {
            try {
                br.close();
            } catch (IOException ex) {
            }
        }
        return map;
    }

    /**
     * 读写文件
     *
     * @param file
     * @return
     */
    public static void unread(File file,String url) {
//        判读txt
        String name = file.getName();
        if (name.contains("txt")) {
            File file1 = new File(url+File.separator +name);
            BufferedReader br = null;
            BufferedWriter writer = null;
            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "utf-8"));
                writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file1)));
                String line = null;
                LinkedList<String[]> ll = new LinkedList<>();
                int count = 0;
                while ((line = br.readLine()) != null) {
//                业务处理
                    String s = lineStringPross(line);
                    writer.write(s);
                    writer.write("\n");
                    writer.flush();
                }
            } catch (FileNotFoundException | UnsupportedEncodingException ex) {
            } catch (IOException ex) {
            } finally {
                try {
                    writer.close();
                    br.close();
                } catch (IOException ex) {
                }
            }
        }
    }

    //读取的line 数据进行处理
    public static String lineStringPross(String line) {
        String[] linetxt = line.split(" ");
        //把 首字9替换成3
        if (linetxt[0].toString().equals("9")) {
            linetxt[0] = "3";
        }
        List<String> s = new ArrayList<>();
        Arrays.stream(linetxt).forEach(i -> {
            s.add(i);
        });
        String join = StringUtils.join(s, " ");

        return join;
    }
}
